package cn.marinda.listener;

import cn.marinda.entity.Server;
import cn.marinda.gui.impl.SettingSelectServerCoreFileHelperImpl;
import cn.marinda.gui.impl.SuccessSettingSelectCoreDialogImpl;
import cn.marinda.util.FileUtils;
import cn.marinda.util.SwingUtils;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

/**
 * 设置默认服务器核心事件
 *
 * @author marinda
 * @date 2022/08/02
 */
public class SettingDefaultServerCoreEvent implements ActionListener {

    private JComboBox jComboBox;
    public SettingDefaultServerCoreEvent(JComboBox jComboBox){
        this.jComboBox = jComboBox;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        File batFile = new File(System.getProperty("user.dir")+"/"+ Server.SERVER_DIR.getPath(),"start.bat");
        String coreName = jComboBox.getSelectedItem().toString();
        FileUtils.moveCoreByName(coreName);
        this.validHighVersionCore(batFile,coreName);
//        FileUtils.writeStartServerBat(batFile,coreName);
        SuccessSettingSelectCoreDialogImpl.instace.close();
        SuccessSettingSelectCoreDialogImpl.instace.open();
        SwingUtils.settingSelectServerCoreFileHelper.close();
    }

    /**
     * 校验高版本核心
     *
     * @param bat      蝙蝠
     * @param coreName 核心名字
     */
    private void validHighVersionCore(File bat,String coreName){
        String version = FileUtils.getServerVersion(coreName);
        Double coreVersion = Double.parseDouble(version);
        if(coreVersion >=1.16 && coreVersion != 1.6 && coreVersion != 1.7 && coreVersion != 1.8 && coreVersion != 1.9){
            FileUtils.appendToHightVersionServerBat(bat,coreName);
        }else{
            FileUtils.writeStartServerBat(bat,coreName);
        }
    }
}
